// Copyright (C) 2012 Yury Bychenkov (http://bychenkov.com).
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//  - Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//  - Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//  - Neither the name of Yury Bychenkov nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

inline LogChunk::Iterator LogChunk::iterator(uint bucketIndex) const {
	return Iterator(*this, bucketIndex);
}

inline LogChunk::Iterator::Iterator() {
}

inline const uint8* LogChunk::Iterator::getValue() const {
	return value;
}

template <typename ElementTraits> 
bool LogChunk::Iterator::skip() {
	if (offset >= size) {
		return false;
	}

	value = data + offset;
	valueIndex++;

	offset += ElementTraits::sizeOf(value);

	if (offset >= size) {
		skipBlock();
	}

	return true;
}

template <typename ElementTraits> 
bool LogChunk::Iterator::skip(uint skipCount) {
	for ( ; skipCount > 0; skipCount--) {
		if (!skip<ElementTraits>()) {
			return false;
		}

	}
	return true;
}

inline uint LogChunk::Iterator::getValueIndex() const {
	return valueIndex;
}


inline LogChunkProvider::LogChunkProvider(const Schema& schema_, const LogChunk& chunk_): schema(schema_), chunk(chunk_) {
}

inline const Schema& LogChunkProvider::getSchema() const {
	return schema;
}

inline uint LogChunkProvider::getCardinality(uint columnIndex) const {
	return 0;
}

template <typename ElementTraits>
int LogChunkProvider::find(uint columnIndex, const uint8* value) const {
	return 0;
}

inline bool LogChunkProvider::isIndexAvailable(uint columnIndex) const {
	return false;
}

inline LogChunkProvider::Cursor::Cursor(const LogChunkProvider& p, const uint* columnMapping, uint columnCount, const uint32*, uint):
	valueIndex(0) 
{
	for (uint i = 0; i < columnCount; i++) {
		iterators[i] = p.chunk.iterator(columnMapping[i]);
	}
	firstColumnType = p.getSchema().getColumn(columnMapping[0]).type;
}

inline uint32 LogChunkProvider::Cursor::getIndex(uint columnIndex) {
	return 0;
}


inline const uint8* LogChunkProvider::Cursor::getString(uint columnIndex) {
	LogChunk::Iterator& it = iterators[columnIndex];
	it.skip<StringTraits>(valueIndex - it.getValueIndex());
	return it.getValue();
}

inline const uint8* LogChunkProvider::Cursor::getDecimal(uint columnIndex) {
	LogChunk::Iterator& it = iterators[columnIndex];
	it.skip<DecimalTraits>(valueIndex - it.getValueIndex());
	return it.getValue();
}

inline const uint8* LogChunkProvider::Cursor::getInteger(uint columnIndex) {
	LogChunk::Iterator& it = iterators[columnIndex];
	it.skip<IntegerTraits>(valueIndex - it.getValueIndex());
	return it.getValue();
}



inline bool LogChunkProvider::Cursor::next() {
	valueIndex++;

	LogChunk::Iterator& it = iterators[0];
	switch (firstColumnType) {
		case Schema::STRING:
			if (!it.skip<StringTraits>()) {
			   	return false;
			}
			break;

		case Schema::INTEGER:
			if (!it.skip<IntegerTraits>()) {
			   	return false;
			}
			break;

		case Schema::DECIMAL:
			if (!it.skip<DecimalTraits>()) {
			   	return false;
			}
			break;
	}
	
	return true;
}

inline uint LogChunkProvider::Cursor::getValueIndex() const {
	return valueIndex;
}
